package com.ruoyi.school;

import com.ruoyi.common.utils.SecurityUtils;
import org.openapi.service.IDbService;
import org.openapi.vo.TableQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class SchoolService {
    @Autowired
    private IDbService dbService;

    /**
     * 查询当前学期
     * @return
     */
    public Map getSemester(){
        TableQuery tableQuery = new TableQuery();
        tableQuery.setTable("base_semester");
        tableQuery.addData("begin_date", "@now()");
        tableQuery.addData("end_date", "@now()");
        tableQuery.setExp("begin_date", "lte");
        tableQuery.setExp("end_date", "gte");
        return dbService.find(tableQuery);
    }


    /**
     * 查询当前学生/老师信息
     * @return
     */
    public Map getUserInfo(){
        Long userId = SecurityUtils.getLoginUser().getUserId();
        TableQuery tableQuery = new TableQuery();
        tableQuery.setTable("base_teacher");
        tableQuery.addData("user_id", userId);
        Map data = dbService.find(tableQuery);
        if(data == null){
            tableQuery.setTable("base_student");
            tableQuery.addData("user_id", userId);
            data = dbService.find(tableQuery);
            if(data != null){
                data.put("_identity", "student");
                data.put("class_id", data.get("class_id"));

                // 班级详细
                tableQuery.getData().remove("user_id");
                tableQuery.setTable("base_class");
                tableQuery.addData("id", data.get("class_id"));
                Map dd = dbService.find(tableQuery);
                data.put("grade_id", dd.get("grade_id"));
                data.put("class_room", dd.get("room_id"));
                data.put("master_user_id", dd.get("user_id"));
            }
        }else{
            data.put("_identity", "teacher");
            data.put("dept_id", data.get("dept_id"));
        }
        return data;
    }
}
